var util = require('../../utils/util.js')
var app = getApp();
const db = wx.cloud.database();

Page({
data:{
  userName:"",
  word:null,
  serveTime:0,
  hours:0,
  minute:0,
  timeText:"00小时00分钟"
},


onLoad:function(){

//判断现在时间
var now = new Date();
var hour = now.getHours();
var word = this.data.word;

if (hour < 6) {
  word = '凌晨好'
  this.setData({
    word:word
  })
} else if (hour < 9) {
  word = '早上好'
  this.setData({
    word:word
  })
} else if (hour < 12) {
  word = '上午好'
  this.setData({
    word:word
  })
} else if (hour < 14) {
  word = '中午好'
  this.setData({
    word:word
  })
} else if (hour < 17) {
  word = '下午好'
  this.setData({
    word:word
  })
  console.log(this.data.word)
} else if (hour < 19) {
  word = '傍晚好'
  this.setData({
    word:word
  })
} else{
  word = '晚上好'
  this.setData({
    word:word
  })
}

//获取用户名
wx.cloud.callFunction({ //调用云函数获取openid
  name: "getOpenId",
  complete: res => {
    db.collection("userInfo").where({
      _openid: res.result.openid //进行筛选
    }).get({
      success: res => {
        console.log("查找用户注册信息成功", res.data)
        if (res.data.length === 1) {
          this.setData({
             userName:res.data[0].realName,
             serveTime:res.data[0].serveTime
            })

            this.timeTrans();
        } else {
          this.setData({
            userName:"志愿者",
            serveTime:"0小时00分钟"
            })
            

        }
      },
      fail: res => {
        console.log("查找用户注册信息失败", res)
      }
    })
  }
});


  wx.getSetting({
    success: function (res) {
      if (res.authSetting['scope.userInfo']) {
        wx.getUserInfo({
          success: function (res) {
            wx.cloud.callFunction({    //调用云函数获取openid
              name:"getOpenId",
              complete:res=>{
                db.collection("userInfo").where({
                  _openid: res.result.openid //进行筛选
                }).get({
                  success:res=>{
                    if(res.data.length === 1){
                      that.setData({
                        re: true
                      })
                      wx.navigateTo({
                        url: '/pages/home/home',
                      })
                    }else {
                      that.setData({
                        re: false
                      })
                    }
                  }
                })
              }
            });
          }
        });
      }else{
        wx.redirectTo({
          url: '../mine/login'
        });
      }
    }
  })
  
},
goMx:function(){
  // wx.showToast({
  //   title: '此功能暂未开放',
  //   icon: 'success',
  //   duration: 2000

  // })
 
  wx.navigateTo({
    url: '/pages/history/history',
  })
},
goZz:function(){
  // wx.showToast({
  //   title: '此功能暂未开放',
  //   icon: 'none',
  //   duration: 2000

  // })
  wx.navigateTo({
    url: '/pages/transfer/transfer',
  })

},

//将服务时长的分钟数转换为可读的 xx小时yy分钟,注意数据类型转换
timeTrans:function(){
  var serveTimes=Number(this.data.serveTime);
  var hours=parseInt(serveTimes/60);//将hours取整
  var minute=0;
  console.log("serveTimes为",serveTimes)
  
  //能整除说明没有有分钟数
  if(serveTimes%60==0){
    minute=0;
  }else{
    //不能整除，计算分钟数
    minute=Number(serveTimes%60)
  }

  var timeText=hours+" 小时 "+minute+" 分钟 ";
  this.setData({
    timeText:timeText
  })
  console.log(this.data.timeText)
}


})